package com.yc.service;

import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Slf4j
@Transactional  // 事务
public class EmailBizImpl implements EmailBiz {
    @Value("${from}")
    private String from;
    @Autowired
    private JavaMailSender  javaMailSender;

    @Override
    @Async
    public void sendMail(String to, String subject, String context) throws Exception {
        MimeMessage mm = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mm, true,"utf-8");
        try {
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(context, true);
            javaMailSender.send(mm);
            log.info("发送邮件成功");
        }catch (Exception  e){
            e.printStackTrace();
            log.info("发送邮件失败:"+e.getMessage());
        }

    }
    @Override
    public long getMailCount() {
        //获取邮件数量
//        return oPrecordDao.selectCount(null);
        return 0;
    }
}
